//
//  SHTabBarViewController.m
//  ShinyOrder
//
//  Created by 王方伟 on 16/7/22.
//  Copyright © 2016年 ShinyHub. All rights reserved.
//

#import "SHTabBarViewController.h"
#import "SHLiveEntViewController.h"
#import "SHNavigationController.h"
#import "SHLinkViewController.h"
#import "SHIMViewController.h"
#import "SHMeViewController.h"
#import "SHTabBarView.h"

@interface SHTabBarViewController ()<TabBarViewDelegage>
@property (nonatomic, weak) SHTabBarView *customTabBar;
@end

@implementation SHTabBarViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //添加自定义TabBar
    [self initTabBar];
    //添加所有的子控件
    [self initAllViewController];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [UIApplication sharedApplication].statusBarHidden = NO;
}


//删除默认的TabBar
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    for (UIView *childView in self.tabBar.subviews) {
        if ([childView isKindOfClass:[UIControl class]]) {
            [childView removeFromSuperview];
        }
    }
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    
    for (UIView *childView in self.tabBar.subviews) {
        if ([childView isKindOfClass:[UIControl class]]) {
            [childView removeFromSuperview];
        }
    }
}

/**
 *  自定义TabBar
 */
-(void)initTabBar{
    SHTabBarView *customTabBar=[[SHTabBarView alloc]init];
    customTabBar.frame=self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar=customTabBar;
    //设置代理
    customTabBar.delegate=self;
}

/**
 *  TabBar的代理切换控制器
 *
 *  @param tabBar tabBar
 *  @param from   当前的控制器
 *  @param to     要跳转的控制器
 */
-(void)tabBar:(SHTabBarView *)tabBar didSelectedButtonFrom:(long)from to:(long)to{
    self.selectedIndex=to;
    [[NSNotificationCenter defaultCenter] postNotificationName:SelectedTabBarItemNotification object:nil userInfo:nil];
}


- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    [super setSelectedIndex:selectedIndex];
    _customTabBar.selectedIndex = selectedIndex;
}

/**
 *  创建所有子控件
 */
-(void)initAllViewController{
    
    //IM
    SHIMViewController *imVC = [[SHIMViewController alloc]init];
    [self initChickViewController:imVC title:@"会话" imageName:@"icon_message_normal" selectImageName:@"icon_message_pressed"];
    
    //通讯录
    SHLinkViewController *liveVC = [[SHLinkViewController alloc] init];
    [self initChickViewController:liveVC title:@"通讯录" imageName:@"icon_contact_normal" selectImageName:@"icon_contact_pressed"];
    
    //互动直播
    SHLiveEntViewController *liveEntVC = [SHLiveEntViewController alloc];
    [self initChickViewController:liveEntVC title:@"互动" imageName:@"icon_chatroom_normal" selectImageName:@"icon_chatroom_pressed"];
    
    //我的
    SHMeViewController *meVC = [[SHMeViewController alloc] init];
    [self initChickViewController:meVC title:@"我的" imageName:@"icon_setting_normal" selectImageName:@"icon_setting_pressed"];
   
}


/**
 *  创建一个子控件
 *
 *  @param childVc         子控件名称
 *  @param title           标题
 *  @param imageName       默认图片
 *  @param selectImageName 选中时的图片
 */
-(void)initChickViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName{
    //设置子控制器的属性
    childVc.title=title;
    childVc.tabBarItem.image=[UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage=[[UIImage imageNamed:selectImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //将子控制器添加到导航控制器中
    SHNavigationController *nc=[[SHNavigationController alloc]initWithRootViewController:childVc];
    [self addChildViewController:nc];
    
    [self.customTabBar addTabBarButtonWith:childVc.tabBarItem];
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
